package com.yh.until;



import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class DBuntil {
    private static String DRI;
    private static String URL;
    private static String USE;
    private static String PWD;
    private static Connection conn;
    /**
     * 获取连接
     * @return
     */
    public static Connection getConn() {


        try {
            readcon();
            Class.forName(DRI);
            conn = DriverManager.getConnection(URL,USE,PWD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return conn;
    }
private static void  readcon() throws IOException {
       //读取配置文件
    InputStream in=DBuntil.class.getClassLoader().getResourceAsStream("databases.properties");
    Properties pro=new Properties();
    pro.load(in);
    DRI=pro.getProperty("driverClass");
    URL=pro.getProperty("url");
    USE=pro.getProperty("use");
    PWD=pro.getProperty("pwd");

    }
    /**
     * 关闭数据库
     * @param conn
     * @param st
     * @param rs
     */
    public static void clossAll(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
